﻿using System.Collections.Generic;
using System.Linq;

namespace com.foxmail.wyyuan1991.Assignment.Host
{
    //输入生成器
    public static class DataGenerator
    {
        /// <summary>
        /// 路网生成测试
        /// </summary>
        /// <returns></returns>
        public static INetwork GenTestNetwork1()
        {
            Network net = new Network()
            {
                ArcList = new List<IArc>(),
                NodeList = new List<INode>()
            };
            for (int i = 0; i <= 5; i++)
            {
                Node n = new Node("n" + (i + 1).ToString());
                net.NodeList.Add(n);
            }

            net.ArcList.Add(new Arc() { ID = "a1", SNode = net.NodeList[0], ENode = net.NodeList[2], PerformaceFunction = delegate (double load) { return 2 * load + 1; } });
            net.ArcList.Add(new Arc() { ID = "a2", SNode = net.NodeList[1], ENode = net.NodeList[2], PerformaceFunction = delegate (double load) { return 2 * load + 1; } });
            net.ArcList.Add(new Arc() { ID = "a3", SNode = net.NodeList[2], ENode = net.NodeList[3], PerformaceFunction = delegate (double load) { return 2 * load + 1; } });
            net.ArcList.Add(new Arc() { ID = "a4", SNode = net.NodeList[3], ENode = net.NodeList[4], PerformaceFunction = delegate (double load) { return 2 * load + 1; } });
            net.ArcList.Add(new Arc() { ID = "a5", SNode = net.NodeList[3], ENode = net.NodeList[5], PerformaceFunction = delegate (double load) { return 2 * load + 1; } });

            foreach (INode n in net.NodeList)
            {
                n.ArcList = net.ArcList.Where(i => i.SNode == n).ToList();
            }
            return net;
        }
        public static INetwork GenTestNetwork2()
        {
            Network net = new Network()
            {
                ArcList = new List<IArc>(),
                NodeList = new List<INode>()
            };
            for (int i = 0; i <= 2; i++)
            {
                Node n = new Node("n" + (i + 1).ToString());
                net.NodeList.Add(n);
            }

            net.ArcList.Add(new Arc() { ID = "a1", SNode = net.NodeList[0], ENode = net.NodeList[1], PerformaceFunction = x => x * x });//罚函数???
            net.ArcList.Add(new Arc() { ID = "a2", SNode = net.NodeList[0], ENode = net.NodeList[1], PerformaceFunction = delegate (double load) { return load + 5; } });
            net.ArcList.Add(new Arc() { ID = "a3", SNode = net.NodeList[0], ENode = net.NodeList[1], PerformaceFunction = delegate (double load) { return load * 2; } });
            net.ArcList.Add(new Arc() { ID = "a4", SNode = net.NodeList[0], ENode = net.NodeList[1], PerformaceFunction = delegate (double load) { return 9999; } });
            net.ArcList.Add(new Arc() { ID = "a5", SNode = net.NodeList[1], ENode = net.NodeList[2], PerformaceFunction = delegate (double load) { return load; } });

            foreach (INode n in net.NodeList)
            {
                n.ArcList = net.ArcList.Where(i => i.SNode == n).ToList();
            }
            return net;
        }
        //Generalize a Constraint Network
        public static INetwork GenTestNetwork3()
        {
            //构建路网
            Network net = new Network()
            {
                ArcList = new List<IArc>(),
                NodeList = new List<INode>()
            };
            for (int i = 0; i <= 1; i++)
            {
                Node n = new Node("n" + (i + 1).ToString());
                net.NodeList.Add(n);
            }

            //增加弧
            net.ArcList.Add(new Arc() { ID = "a1", SNode = net.NodeList[0], ENode = net.NodeList[1], PerformaceFunction = x => x * x });//罚函数???
            net.ArcList.Add(new Arc() { ID = "a2", SNode = net.NodeList[0], ENode = net.NodeList[1], PerformaceFunction = x => 0.5 * x });
            net.ArcList.Add(new Arc() { Capacity = 5 ,ID = "a3", SNode = net.NodeList[0], ENode = net.NodeList[1], PerformaceFunction = x => x + 1 });

            foreach (INode n in net.NodeList)
            {
                n.ArcList = net.ArcList.Where(i => i.SNode == n).ToList();
            }
            return net;
        }
        //Generalize a Constraint Network
        public static INetwork GenTestNetwork4()
        {
            Network net = new Network()
            {
                ArcList = new List<IArc>(),
                NodeList = new List<INode>()
            };
            for (int i = 0; i <= 5; i++)
            {
                Node n = new Node("n" + (i + 1).ToString());
                net.NodeList.Add(n);
            }

            net.ArcList.Add(new Arc() { ID = "a1", SNode = net.NodeList[0], ENode = net.NodeList[2], PerformaceFunction = delegate (double load) { return 2 * load + 1; } });
            net.ArcList.Add(new Arc() { ID = "a2", SNode = net.NodeList[1], ENode = net.NodeList[2], PerformaceFunction = delegate (double load) { return 2 * load + 1; } });
            net.ArcList.Add(new Arc() { ID = "a3", SNode = net.NodeList[2], ENode = net.NodeList[3], PerformaceFunction = x => 3, Capacity = 10 });
            //net.ArcList.Add(new Arc() { ID = "a3-1", SNode = net.NodeList[2], ENode = net.NodeList[3], PerformaceFunction = x => 5, Capacity = 200});
            net.ArcList.Add(new Arc() { ID = "a4", SNode = net.NodeList[3], ENode = net.NodeList[4], PerformaceFunction = delegate (double load) { return 2 * load + 1; } });
            net.ArcList.Add(new Arc() { ID = "a5", SNode = net.NodeList[3], ENode = net.NodeList[5], PerformaceFunction = delegate (double load) { return 2 * load + 1; } });
            net.ArcList.Add(new Arc() { ID = "a6", SNode = net.NodeList[2], ENode = net.NodeList[5], PerformaceFunction = delegate (double load) { return 2 * load + 5; }, Capacity = 80 });

            foreach (INode n in net.NodeList)
            {
                n.ArcList = net.ArcList.Where(i => i.SNode == n).ToList();
            }
            return net;
        }
        public static INetwork GenTestNetwork5()
        {
            Network net = new Network()
            {
                ArcList = new List<IArc>(),
                NodeList = new List<INode>()
            };
            for (int i = 0; i <= 4; i++)
            {
                Node n = new Node("n" + (i).ToString());
                net.NodeList.Add(n);
            }

            net.ArcList.Add(new Arc() { ID = "a0", SNode = net.NodeList[0], ENode = net.NodeList[2], PerformaceFunction = x => 3 });
            net.ArcList.Add(new Arc() { ID = "a1", SNode = net.NodeList[1], ENode = net.NodeList[2], PerformaceFunction = x => 3 });
            net.ArcList.Add(new Arc() { ID = "a2", SNode = net.NodeList[2], ENode = net.NodeList[3], PerformaceFunction = x => 3, Capacity = 15 });
            net.ArcList.Add(new Arc() { ID = "a3", SNode = net.NodeList[1], ENode = net.NodeList[3], PerformaceFunction = x => 7, Capacity = 5 });

            net.ArcList.Add(new Arc() { ID = "v_0->2", SNode = net.NodeList[0], ENode = net.NodeList[2], PerformaceFunction = x => 99 });
            net.ArcList.Add(new Arc() { ID = "v_1->2", SNode = net.NodeList[1], ENode = net.NodeList[2], PerformaceFunction = x => 99 });
            net.ArcList.Add(new Arc() { ID = "v_0->3", SNode = net.NodeList[0], ENode = net.NodeList[3], PerformaceFunction = x => 99 });
            net.ArcList.Add(new Arc() { ID = "v_1->3", SNode = net.NodeList[1], ENode = net.NodeList[3], PerformaceFunction = x => 99 });
            net.ArcList.Add(new Arc() { ID = "v_2->3", SNode = net.NodeList[2], ENode = net.NodeList[3], PerformaceFunction = x => 99 });

            foreach (INode n in net.NodeList)
            {
                n.ArcList = net.ArcList.Where(i => i.SNode == n).ToList();
            }
            return net;
        }
        public static IDemand GenTestFlow(INetwork net)
        {
            IDemand f = new Flow() { Network = net, ODFlowList = new List<IDistributionOD>() };
            foreach (INode n in net.NodeList)
            {
                foreach (INode m in net.NodeList)
                {
                    if (n != m)
                    {
                        ODFlow odf = new ODFlow()
                        {
                            OrgNode = n,
                            DesNode = m,
                            Num = 10
                        };
                        f.ODFlowList.Add(odf);
                    }
                }
            }
            return f;
        }
    }

}
